$(function () {
    var userId = localStorage.getItem("userId");
    var token = localStorage.getItem("jwt");
    $.ajax({
        url: "/class?all=true",
        type: "get",
        headers: {"Authorization": "Bearer " + token},
        success: function (data) {
            console.log(data);
            if (data.length != undefined) {
                for (var i = 0; i < data.length; i++) {
                    $(".content").append(
                        "<div class=\"main_box_right_content\" id=" + data[i].id + ">" +
                        "  <h3 class=\"classtitle\">" + data[i].courseName + "" +
                        "    <button id='selectClass'>选择课程</button>" +
                        "  </h3>" +
                        "  <div class=\"divideline\"></div>" +
                        "  <div class=\"classinfo\">" +
                        "  <table class=\"table\">" +
                        "    <tr>" +
                        "      <td class=\"tabletext\">班级：<span>" + data[i].name + "</span></td>" +
                        "      <td class=\"tabletext\">课程地点：<span>" + data[i].site + "</span></td>" +
                        "    </tr>" +
                        "    <tr>" +
                        "      <td class=\"tabletext\">班级人数：<span>" + data[i].numStudent + "</span></td>" +
                        "      <td class=\"tabletext\">时间：<span>" + data[i].time + "</span></td>" +
                        "    </tr>" +
                        "  </table>" +
                        "  </div>" +
                        "</div>");
                }
            }
        }
    });
    //点击选课按钮后的情况
    $(".body").on("click", "#selectClass", function () {
        if (confirm("确认选择该班级")) {
            var classId = $(this).parent().parent().attr("id");
            var student = {
                id: userId
            };
            console.log(classId);
            $.ajax({
                //url: "http://rap2api.taobao.org/app/mock/933/POST/class/34/student",
                url: "/class/"+classId+"/student",
                type: "POST",
                headers: {"Authorization": "Bearer " + token},
                contentType: "application/json",
                data: JSON.stringify(student),
                success: function (data) {
                    console.log(data);
                    alert("选课成功");
                    $("#selectClass").hide();
                }
            });
        }
    })

    //点击查询后的情况
    $("#search").on("click", function () {
        console.log("lala");
        var teacherName = $("input[name='teacher']").val();
        var courseName = $("input[name='course']").val();
        var url = "/class";
        if (teacherName != "") {
            url += "?courseTeacher=" + teacherName;
        }
        if (courseName != "") {
            if (url == "/class") {
                url += "?courseName=" + courseName;
            } else {
                url += "&courseName=" + courseName;
            }
        }
        $.ajax({
            url: url,
            type: "get",
            headers: {"Authorization": "Bearer " + token},
            success: function (data) {
                $(".main_box_right_content").remove();
                if (data.length != undefined) {
                    for (var i = 0; i < data.length; i++) {
                        $(".content").append(
                            "<div class=\"main_box_right_content\" id=" + data[i].id + ">" +
                            "  <h3 class=\"classtitle\">" + data[i].courseName + "" +
                            "    <button id='selectClass'>选择课程</button>" +
                            "  </h3>" +
                            "  <div class=\"divideline\"></div>" +
                            "  <div class=\"classinfo\">" +
                            "  <table class=\"table\">" +
                            "    <tr>" +
                            "      <td class=\"tabletext\">班级：<span>" + data[i].name + "</span></td>" +
                            "      <td class=\"tabletext\">课程地点：<span>" + data[i].site + "</span></td>" +
                            "    </tr>" +
                            "    <tr>" +
                            "      <td class=\"tabletext\">班级人数：<span>" + data[i].numStudent + "</span></td>" +
                            "      <td class=\"tabletext\">时间：<span>" + data[i].time + "</span></td>" +
                            "    </tr>" +
                            "  </table>" +
                            "  </div>" +
                            "</div>");
                    }
                }
            }
        })
    })
});